package com.vueapi.controller.admin;

import com.vueapi.annotation.OperationLog;
import com.vueapi.constant.ErrorCodeEnum;
import com.vueapi.exception.BlogException;
import com.vueapi.utils.QiniuUtils;
import com.vueapi.utils.ResultUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Classname AdminUploadController
 * @Description TODO
 * @Created by 赵宏建
 * @Date 2022/9/22 8:45
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/admin/upload")
public class AdminUploadController {

    @Resource
    private QiniuUtils qiniuUtils;

    @OperationLog(name = "上传文件")
    @PostMapping
    public ResultUtils upload(@RequestParam("image") MultipartFile file) {
        try {
            System.out.println("七牛云开始上传文件...");
            String url = qiniuUtils.saveImage(file);
            if(url == null){
                return ResultUtils.fail("上传图片失败...");
            }
            System.out.println("七牛云上传文件成功！");
            return ResultUtils.success(url);
        } catch (IOException e) {
            throw new BlogException(ErrorCodeEnum.UPLOAD_IMG_ERROR.getCode(),ErrorCodeEnum.UPLOAD_IMG_ERROR.getMsg());
        }
    }
}
